package com.fingard.dsp.bank.directbank.cmb01;

import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.directbank.cmb01.CMB01Base;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CMB01OtherElse extends CMB01Base implements IOtherElse {
	public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception{
		RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
		if(p_reqElse.reqHeader.transType.equals("ListMode")){
			String tmpBUSCOD = p_reqElse.xmlReader.getSimpleText("BUSCOD");
			retAts.writeBankRetStr(execListMode(tmpBUSCOD));
		}
		else if(p_reqElse.reqHeader.transType.equals("QueryAgentList")){
			String tmpBUSCOD = p_reqElse.xmlReader.getSimpleText("BUSCOD");
			retAts.writeBankRetStr(execQueryAgentList(tmpBUSCOD));
		}
		else if (p_reqElse.reqHeader.transType.equals("NTAGTLS2")) {
			String tmpBUSCOD = p_reqElse.xmlReader.getSimpleText("BUSCOD");
			String tmpACCNBR = p_reqElse.xmlReader.getSimpleText("ActNum");
			
			retAts.writeBankRetStr(execNTAGTLS2(tmpBUSCOD, tmpACCNBR));
		}
		else{
			retAts.writeBankRetStr("未知的TransType：" + p_reqElse.reqHeader.transType);
		}
		
		return retAts;
	}
	//查询可经办的业务模式信息查询
	public String execListMode(String p_BUSCOD) throws Exception{
		try {
			CMB01Header tmpBnkHeader = new CMB01Header(); 
			tmpBnkHeader.FUNNAM = "ListMode";
			XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
			//写入通用的头部节点
			writeCommonHeader(xmlWriter, tmpBnkHeader);
			xmlWriter.writeStartElement("SDKMDLSTX");
			xmlWriter.writeElementString("BUSCOD", p_BUSCOD);
			xmlWriter.writeEndDocument();
			String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
			return tmpRetBkStr[1];
		} catch (Exception e) {
			this.WriteBankLogLn(e);
			return LogHelper.getStackTrace(e);
		}
	}
	
	//代发代扣交易代码查询
	public String execQueryAgentList(String p_BUSCOD) throws Exception{
		try {
			CMB01Header tmpBnkHeader = new CMB01Header(); 
			tmpBnkHeader.FUNNAM = "QueryAgentList";
			XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
			//写入通用的头部节点
			writeCommonHeader(xmlWriter, tmpBnkHeader);
			xmlWriter.writeStartElement("SDKAGTTSX");
			xmlWriter.writeElementString("BUSCOD", p_BUSCOD);
			xmlWriter.writeEndDocument();
			String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
			return tmpRetBkStr[1];
		} catch (Exception e) {
			this.WriteBankLogLn(e);
			return LogHelper.getStackTrace(e);
		}
	}
	
	/**
	 * 新查询代发代扣交易代码
	 * 
	 * @param p_BUSCOD 业务代码
	 * @param p_ACCNBR 账号
	 * @return 招行响应
	 * @throws Exception 交互异常
	 */
	public String execNTAGTLS2(String p_BUSCOD, String p_ACCNBR) throws Exception{
		try {
			CMB01Header tmpBnkHeader = new CMB01Header(); 
			tmpBnkHeader.FUNNAM = "NTAGTLS2";
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHeader(xmlWriter, tmpBnkHeader);
			
			xmlWriter.writeStartElement("NTAGTLS2X");
			xmlWriter.writeElementString("BUSCOD", p_BUSCOD); /* 业务代码:  N03020-代发, N03030-代扣; */
			xmlWriter.writeElementString("ACCNBR", p_ACCNBR); /* 账户 */
			xmlWriter.writeEndDocument();
			
			String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
			
			return tmpRetBkStr[1];
		} catch (Exception e) {
			this.WriteBankLogLn(e);
			return LogHelper.getStackTrace(e);
		}
	}
	
	public String getBUSMOD(String p_BUSCOD){
		try{
			//String tmpFileCache = FGBiz.dspCenter.rootDirPath+File.separator+ "config"+File.separator+"portfiles"+File.separator+reqHeader.bankCode+File.separator+bankFront.code + "_ListMode_" + p_BUSCOD + ".xml";
			String tmpRetStr = execListMode(p_BUSCOD);
			StringBuilder tmpSbMsg = new StringBuilder();
			XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr, tmpSbMsg);
			if(tmpBkReader != null){
				String tmpRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
				if(tmpRetCode != null && tmpRetCode.equals("0")){
					String tmpBUSMOD = tmpBkReader.getFirstText("/CMBSDKPGK/NTQMDLSTZ/BUSMOD");
					if(tmpBUSMOD != null && tmpBUSMOD.length()>0){
						return tmpBUSMOD;
					}
				}
			}
		}
		catch(Exception e){
			this.WriteBankLogLn(e);
		}
		return "";
	}
}
